package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.PhysiqueAssessmentDao;
import com.itheima.dao.PhysiqueDao;
import com.itheima.dao.RiskAssessmentDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 检查组CRUD操作业务实现类
 *
 * @author W.J.Y
 */
@Service(version = "1.0.0", interfaceClass = RiskAssessmentService.class)
@Transactional
public class RiskAssessmentServiceImpl implements RiskAssessmentService {


    @Resource
    private PhysiqueDao physiqueDao;
    @Resource
    private PhysiqueAssessmentDao physiqueAssessmentDao;
    @Resource
    private RiskAssessmentDao riskAssessmentDao;
    @Resource
    private MemberDao memberDao;

    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //调用dao层查询语句
        Page<RiskAssessmentReport> page = riskAssessmentDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询所有体质
     *
     * @return 体质集合
     */
    public List<Physique> findAllPhy() {
        List<Physique> physiqueList = physiqueDao.findAllPhy();
        for (Physique physique : physiqueList) {
            List<PhyQuestion> phyQuestionList = physiqueDao.findPhyQuestionByPid(physique.getId());
            physique.setQuestionList(phyQuestionList);
        }
        return physiqueList;
    }

    /**
     * 删除风险测评
     * @param fileNumber 风评fileNumber
     */
    public void delete(String fileNumber) {
        riskAssessmentDao.delete(fileNumber);
    }

    /**
     * @param fileNumber 会根据档案号查一下member性别年龄
     * @param report     前台传来的报告体检报告 一会进行风险评估
     * @return result
     */
    @Override
    public Result addRiskAssessment(String fileNumber, List<Report> report) {
        Member member = memberDao.findByFileId(Long.parseLong(fileNumber));
        int age = member.getAge();
        String sex = member.getSex();
        Date assTime = new Date();
        for (Report detailReport : report) {
            RiskAssessmentReport riskAssessmentReport = new RiskAssessmentReport();

            Float result = Float.parseFloat(detailReport.getResult());
            Float leftCritical = Float.parseFloat(detailReport.getLeftCritical());
            Float rightCritical = Float.parseFloat(detailReport.getRightCritical());
            if (result >= leftCritical && result < rightCritical) {
                riskAssessmentReport.setAssessment("正常");
            } else if (result < leftCritical) {
                riskAssessmentReport.setAssessment("低");
            } else {
                riskAssessmentReport.setAssessment("高");
            }
            riskAssessmentReport.setAge(age);
            riskAssessmentReport.setSex(sex);
            riskAssessmentReport.setAssTime(assTime);
            riskAssessmentReport.setName(member.getName());
            riskAssessmentReport.setFileNumber(fileNumber);
            riskAssessmentReport.setResult(detailReport.getResult());
            riskAssessmentReport.setCheckItem(detailReport.getCheckItem());
            riskAssessmentDao.addRiskAssessment(riskAssessmentReport);
        }
        return new Result(true, "生成成功", null);
    }

    /**
     * 根据fileNumber 查找体检报告
     *
     * @param fileNumber 档案号
     * @return result
     */
    @Override
    public Result findReportByFileNumber(String fileNumber) {
        List<Report> reportList = riskAssessmentDao.findReportByFileNumber(fileNumber);
        if (reportList.size() > 0) {

            return new Result(true, "查询体检报告成功", reportList);
        }
        return new Result(false, "没有该体检报告");
    }


    /**
     * 根据ID查询体质测评
     *
     * @param fileNumber 体测fileNumber
     * @return 体测实例
     */
    public List<RiskAssessmentReport> findById(String fileNumber) {
        return riskAssessmentDao.findRiskReportByFileNumber(fileNumber);
    }


    /**
     * 新增检查组
     * @param checkGroup 检查项实例
     */
   /* @Override
    public void add(CheckGroup checkGroup) throws BusinessException {
        Integer count = checkGroupDao.findByNameOrCode(checkGroup);
        if(count>0){
            throw new BusinessException("已存在相同编号、助记码或者名称的检查组，添加失败！");
        }
        //首先添加检查组
        checkGroupDao.add(checkGroup);
        //再添加检查组与检查项的关联
        Integer id = checkGroup.getId();
        List<Integer> checkItemIds = checkGroup.getCheckItemIds();
        for (Integer checkItemId : checkItemIds) {
            checkGroupDao.setCheckGroupAndCheckItem(id,checkItemId);
        }
    }

    *//**
     * 编辑检查组
     * @param checkGroup 检查组实例
     *//*
    @Override
    public void update(CheckGroup checkGroup) throws BusinessException {
        Integer count = checkGroupDao.findByNameOrCode(checkGroup);
        if(count>0){
            throw new BusinessException("已存在相同编号、助记码或者名称的检查组，编辑失败！");
        }
        checkGroupDao.update(checkGroup);
        //首先删除检查组的旧的关联
        checkGroupDao.deleteCheckItemsByCheckGroupId(checkGroup.getId());
        //再添加检查组与检查项的关联
        Integer id = checkGroup.getId();
        List<Integer> checkItemIds = checkGroup.getCheckItemIds();
        for (Integer checkItemId : checkItemIds) {
            checkGroupDao.setCheckGroupAndCheckItem(id,checkItemId);
        }
    }

    */






    /*
    /**
     * 分页查询
     * @param pageNum  查询页码
     * @param pageSize  每页显示条数
     * @param queryString   检查组编码、助记码或者名称
     * @return 分页查询返回结果
     *//*
    @Override
    public PageResult findPage(int pageNum, int pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用dao层查询语句
        Page<CheckGroup> page = checkGroupDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(),page.getResult());
    }*/
}
